package com.mk.config;

import com.mk.common.ThreadLocalUtil;
import com.mk.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.mk.contants.UserConstant.USER_LOGIN_STATE;

/**
 * 登录校验拦截器
 */
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 将当前登录用户信息，存入ThreadLocal中
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        ThreadLocalUtil.set(USER_LOGIN_STATE, loginUser);
        if (loginUser != null)
            log.info("当前登录用户：{}", loginUser.getUserAccount());
        log.info("LoginCheckInterceptor登录校验拦截器: {}", request.getRequestURI());
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
